package com.loeng.framework.security.core.service;

import com.loeng.module.system.api.permission.PermissionApi;
import lombok.AllArgsConstructor;

import java.util.Objects;

import static com.loeng.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 默认的 {@link SecurityFrameworkService} 实现类
 *
 * @author loeng zhuo
 */
@AllArgsConstructor
public class SecurityFrameworkServiceImpl implements SecurityFrameworkService {

    /**
     * 属性，使用构造器注入
     */
    private final PermissionApi permissionApi;

    /**
     * 判断是否有权限
     *
     * @param permission 权限
     * @return 是否
     */
    @Override
    public boolean hasPermission(String permission) {
        return hasAnyPermissions(permission);
    }

    /**
     * 判断是否有权限
     *
     * @param permissions 权限
     * @return 是否
     */
    private boolean hasAnyPermissions(String... permissions) {
        Long userId = getLoginUserId();
        if (Objects.isNull(userId)) {
            return false;
        }
        return permissionApi.hasAnyPermissions(userId, permissions);
    }

}
